package com.ruoyi.bluebera.task;

import com.ruoyi.bluebera.domain.BizOrders;
import com.ruoyi.bluebera.domain.BizSaleStats;
import com.ruoyi.bluebera.domain.BizTxScanLogs;
import com.ruoyi.bluebera.service.IBizOrdersService;
import com.ruoyi.bluebera.service.IBizSaleStatsService;
import com.ruoyi.bluebera.service.IBizTxScanLogsService;
import com.sun.jndi.ldap.Ber;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Component("SaleStatTask")

public class SaleStatTask {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    private static final String EthUsdtchainId = "1";
    private static final String ArbUsdtchainId = "42161";
    private static final String BeraChainId = "80094";

    @Autowired
    private IBizTxScanLogsService bizTxScanLogsService;
    @Autowired
    private IBizSaleStatsService bizSaleStatsService;


    public void sumSales(){
        // 1. get all preprocessing
        BizTxScanLogs bizTxScanLogs = new BizTxScanLogs();
        List<BizTxScanLogs> list = bizTxScanLogsService.selectBizTxScanLogsList(bizTxScanLogs);
        BigDecimal saledAmount = BigDecimal.ZERO;
        BigDecimal usdtErc2Sold = BigDecimal.ZERO;
        BigDecimal beraSold = BigDecimal.ZERO;
        BigDecimal usdtArbSold = BigDecimal.ZERO;
        // 2. process according token
        /*

            usdt_erc20_sold DECIMAL(36, 18) DEFAULT 0 COMMENT '已售USDT-ERC20数量',
            bera_sold DECIMAL(36, 18) DEFAULT 0 COMMENT '已售BERA数量',
            usdt_arb_sold DECIMAL(36, 18) DEFAULT 0 COMMENT '已售USDT-ARB数量',
         */
        logger.info("tx scanlog={}", list);
        for (BizTxScanLogs log : list) {
            logger.info("log amount {} {}", log.getBlueberaAmount(), log.getNetwork().equalsIgnoreCase(EthUsdtchainId));
            saledAmount = saledAmount.add(log.getBlueberaAmount());
            if(log.getNetwork().equalsIgnoreCase(EthUsdtchainId)){
                usdtErc2Sold = usdtErc2Sold.add(log.getBlueberaAmount());
            }
            if(log.getNetwork().equalsIgnoreCase(BeraChainId)){
                beraSold = beraSold.add(log.getBlueberaAmount());
            }
            if(log.getNetwork().equalsIgnoreCase(ArbUsdtchainId)){
                usdtArbSold = usdtArbSold.add(log.getBlueberaAmount());
            }
        }
        logger.info("amount {} {} {} {}", saledAmount, usdtErc2Sold, beraSold, usdtArbSold);
        // 3. update
        BizSaleStats bizSaleStats = new BizSaleStats();
        List<BizSaleStats> logList = bizSaleStatsService.selectBizSaleStatsList(bizSaleStats);
        if(logList.size()>0) {
            bizSaleStats = logList.get(0);
            bizSaleStats.setSoldAmount(saledAmount);
            bizSaleStats.setUsdtErc20Sold(usdtErc2Sold);
            bizSaleStats.setBeraSold(beraSold);
            bizSaleStats.setUsdtArbSold(usdtErc2Sold);
            bizSaleStatsService.updateBizSaleStats(bizSaleStats);
        } else {
            bizSaleStats = new BizSaleStats();
            bizSaleStats.setSoldAmount(saledAmount);
            bizSaleStats.setUsdtErc20Sold(usdtErc2Sold);
            bizSaleStats.setBeraSold(beraSold);
            bizSaleStats.setUsdtArbSold(usdtErc2Sold);
            bizSaleStats.setTotalSupply(new BigDecimal(TaskConstants.TOTAL_SUPPLY));
            bizSaleStatsService.insertBizSaleStats(bizSaleStats);
        }
    }

}

